home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / games / nhak_src.zip / SPLITF.C < prev    next >
C/C++ Source or Header  |  1993-03-16  |  5KB  |  215 lines

  1. /******************************************************************************
  2. *                                          *
  3. *            File Splitter and Re-assembler                  *
  4. *                                          *
  5. *            by Pierre Martineau, 90/05/20                  *
  6. *                                          *
  7. *                 Version 1.1                      *
  8. *                                          *
  9. *             Placed in the public domain                  *
  10. *                                          *
  11. ******************************************************************************/
  12.  
  13. #include <sys\types.h>
  14. #include <sys\stat.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <math.h>
  19.  
  20. FILE *infile, *outfile;
  21. char fname[16];
  22. char chunk_name[16];
  23. int extent = 0;
  24. long hunk_size;
  25. unsigned buflen = 0x8000;
  26. char *buf = 0;
  27.  
  28. main(argc, argv)
  29. int argc;
  30. char *argv[];
  31. {
  32. struct stat stat_buf;
  33. char *cptr;
  34.  
  35.     printf("File Splitter and Re-assembler V1.1, by Pierre Martineau, 90/05/20.\n");
  36.     printf("This program is public domain and may be freely distributed.\n");
  37.     if ((argc < 2) || (argc > 3)) {
  38.     printf("\nUsage: splitf file_to_split [chunk_size]\n");
  39.     printf("       If chunk_size isn't specified, the file will be split\n");
  40.     printf("       into two files of (approximately) equal size.\n\n");
  41.     printf("       splitf dest_file /r\n");
  42.     printf("       /r will re-assemble the parts back into the whole\n");
  43.     printf("       specified by dest_file.\n");
  44.     return;
  45.     }
  46.  
  47. /*  Extract filename from first argumemt  */
  48.  
  49.     if ((cptr = strrchr(argv[1], '\\')) == NULL)
  50.     cptr = argv[1];
  51.     else
  52.     cptr++;
  53.     strcpy(fname, cptr);
  54.     if ((cptr = strchr(fname, '.')) != NULL)
  55.     *++cptr = '\000';
  56.     else
  57.     strcat(fname, ".");
  58.  
  59.     if ((argc == 3) && ((strcmpi(argv[2], "-r") == 0) || (strcmpi(argv[2], "/r") == 0))) {
  60.     getbuf();
  61.     printf("\nRe-assembling %s ...\n\n", argv[1]);
  62.     copy_hunks(argv[1]);
  63.     fclose(outfile);
  64.     freebuf();
  65.     printf("\nDone.\n");
  66.     }
  67.     else {
  68.     getbuf();
  69.     if ((infile = fopen(argv[1], "rb")) == NULL) {
  70.         printf("\nCouldn't open input file!\n");
  71.         return;
  72.     }
  73.     if (stat(argv[1], &stat_buf) != 0) {
  74.         printf("\nBad file handle!\n");
  75.         return;
  76.     }
  77.     if (argc == 3)
  78.         hunk_size = atol(argv[2]);
  79.     else
  80.         hunk_size = (stat_buf.st_size / 2) + 1;
  81.     if (hunk_size < 1) {
  82.         printf("\nInvalid chunk size!\n");
  83.         return;
  84.     }
  85.     printf("\nSplitting %s ...\n\n", argv[1]);
  86.     write_hunks();
  87.     fclose(infile);
  88.     freebuf();
  89.     printf("\nDone.\n");
  90.     }
  91. }
  92.  
  93. write_hunks()
  94. {
  95. long size;
  96. unsigned bufsize;
  97. unsigned numread;
  98.  
  99.     for (;;) {
  100.     if(!next_file()) {
  101.         printf("Too many files, please specify a chunk size that\n");
  102.         printf("will result in fewer than 1000 output files!\n");
  103.         return;
  104.     }
  105.     if ((outfile = fopen(chunk_name, "wb")) == NULL) {
  106.         printf("Unable to create output file %s\n", chunk_name);
  107.         return;
  108.     }
  109.     size = hunk_size;
  110.     numread = 1;
  111.     while(size > 0 && numread /* Work around TC idiot-syncracy */) {
  112.         bufsize = size < buflen ? size : buflen;
  113.         numread = fread(buf, sizeof(char), bufsize, infile);
  114.         if (ferror(infile)) {
  115.         printf("Error while reading input file %s\n", chunk_name);
  116.         fclose(outfile);
  117.         return;
  118.         }
  119.         fwrite(buf, sizeof(char), numread, outfile);
  120.         if (ferror(outfile)) {
  121.         printf("Error while writing output file!\n");
  122.         fclose(outfile);
  123.         return;
  124.         }
  125.         size -= numread;
  126.         if (numread != bufsize) {
  127.         printf("    Writing %ld bytes to %s\n", hunk_size-size, chunk_name);
  128.         fclose(outfile);
  129.         return;
  130.         }
  131.     }
  132.     fclose(outfile);
  133.     printf("    Writing %ld bytes to %s\n", hunk_size-size, chunk_name);
  134.     }
  135. }
  136.  
  137. copy_hunks(filename)
  138. char *filename;
  139. {
  140. unsigned numread;
  141.  
  142.     if(!next_file())
  143.     return;
  144.     if ((infile = fopen(chunk_name, "rb")) == NULL) {
  145.     printf("Nothing to do!\n");
  146.     return;
  147.     }
  148.     if ((outfile = fopen(filename, "wb")) == NULL) {
  149.     printf("Couldn't open output file!\n");
  150.     return;
  151.     }
  152.     for (;;) {
  153.     numread = 1;
  154.     while(!feof(infile) && numread /* Avoid TC problem */) {
  155.         numread = fread(buf, sizeof(char), buflen, infile);
  156.         if (ferror(infile)) {
  157.         printf("Error while reading input file %s\n", chunk_name);
  158.         fclose(infile);
  159.         return;
  160.         }
  161.         fwrite(buf, sizeof(char), numread, outfile);
  162.         if (ferror(outfile)) {
  163.         printf("Error while writing output file!\n");
  164.         fclose(infile);
  165.         return;
  166.         }
  167.     }
  168.     printf("    Copying file %s to output file.\n", chunk_name);
  169.     fclose(infile);
  170.     if(!next_file())
  171.         return;
  172.     if ((infile = fopen(chunk_name, "rb")) == NULL)
  173.         return;
  174.         
  175.     }
  176. }
  177.  
  178. next_file()
  179. {
  180. char num[4];
  181.  
  182.     if (extent > 999)
  183.     return(0);
  184.     strcpy(chunk_name, fname);
  185.     itoa(extent,num, 10);
  186.     if (strlen(num) == 1) {
  187.     strcat(chunk_name, "00");
  188.     strcat(chunk_name, num);
  189.     }
  190.     else if (strlen(num) == 2) {
  191.     strcat(chunk_name, "0");
  192.     strcat(chunk_name, num);
  193.     }
  194.     else
  195.     strcat(chunk_name, num);
  196.     ++extent;
  197.     return(-1);
  198. }
  199.  
  200. getbuf()
  201. {
  202.     while (buflen >= 256 && !(buf = malloc(buflen)))
  203.     buflen >>= 1;
  204.     if (!buf) {
  205.     printf("\nCan't allocate an adequate copy buffer.\n");
  206.     exit(2);
  207.     }
  208. }
  209.  
  210. freebuf()
  211. {
  212.     free(buf);
  213. }
  214.  
  215.